//
//  DigitalUtil.m
//  Rong360
//
//  Created by tianliwei on 21/3/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "DigitalUtil.h"

@implementation DigitalUtil
+ (NSString *)separatedDigitStringWithStr:(NSString *)digitString

{
    NSLog(@"The begin string:%@",digitString);
    if (digitString.length <= 3) {
        return digitString;
    } else {
        NSMutableString *processString = [NSMutableString stringWithString:digitString];
        
        // 1
        int location = processString.length - 3;
        NSMutableArray *processArray = [NSMutableArray array];
        while (location >= 0) {
            NSString *temp = [processString substringWithRange:NSMakeRange(location, 3)];
            
            [processArray addObject:temp];
            if (location < 3 && location > 0)
            {
                NSString *t = [processString substringWithRange:NSMakeRange(0, location)];
                [processArray addObject:t];
            }
            location -= 3;
        }
        //    NSLog(@"processArray = %@",processArray);
        
        // 2
        NSMutableArray *resultsArray = [NSMutableArray array];
        int k = 0;
        for (NSString *str in processArray)
        {
            k++;
            NSMutableString *tmp = [NSMutableString stringWithString:str];
            if (str.length > 2 && k < processArray.count )
            {
                [tmp insertString:@"," atIndex:0];
                [resultsArray addObject:tmp];
            } else {
                [resultsArray addObject:tmp];
            }
        }
        //    NSLog(@"resultsArray = %@",resultsArray);
        
        // 3
        NSMutableString *resultString = [NSMutableString string];
        for (int i = resultsArray.count - 1 ; i >= 0; i--)
        {
            NSString *tmp = [resultsArray objectAtIndex:i];
            [resultString appendString:tmp];
        }
        //    NSLog(@"resultStr = %@",resultString);
        return resultString;
    }
}
@end
